<?php

namespace app\admin\controller;

use think\Request;
use app\admin\controller\Base;
use app\admin\model\PageComponent;
use app\admin\model\Component;
use think\facade\View;
class Page extends Base
{
    private $contents = '';
    private $lists = [];
    private $coms = [];
    Private $comss = [];
    public function getSub($parent_id,&$lists,&$contents){
        foreach($this->coms[$parent_id] as $r){
            $lists[] = $r;
            $cons = '';
            if(isset($this->coms[$r['id']])){
                $this->getSub($r['id'],$lists,$cons);
            }
            $cons = str_replace("{__CONS__}",$cons,$this->comss[$r['component_id']]['content']);
            $contents .= $cons."\r\n";
        }
    }
    public function show(){
        $pc = PageComponent::where('page_id',1)->select();
        $comss = [];
        foreach($pc as $r){
            if(!in_array($r['component_id'],$comss)) $comss[] = $r['component_id'];
            if(!isset($this->coms[$r['parent_id']])) $this->coms[$r['parent_id']] = [];
            $this->coms[$r['parent_id']][] = $r;
        }
        $lists = [];
        $contents = '';
        $comdata = Component::where('id','in',$comss)->select();
        foreach($comdata as $r){
            $this->comss[$r['id']] = $r;
        }
        $this->getSub(0,$lists,$contents);
        return View::display($contents);
    }
}
